//
//  KVFeatureCVC.swift
//  KVBestMicroblog
//
//  Created by 李康卫 on 16/7/22.
//  Copyright © 2016年 李康卫. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"
let itemCount = 4

class KVFeatureCVC: UICollectionViewController {
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    let layout = UICollectionViewFlowLayout()
    init() {
        super.init(collectionViewLayout: layout)
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        //系统cell不能满足
        self.collectionView?.backgroundColor = UIColor.whiteColor()
        self.collectionView?.registerClass(KVFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)
        
        //设置cell的属性
        self.setupCellInfo()
    }
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return itemCount
    }
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell =  collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! KVFeatureCell
        //        new_feature_1
        let imgName: String = "new_feature_\(indexPath.row + 1)"
        
        cell.backgroundImg.image = UIImage(named: imgName)
        
        //第一到3的Button需要隐藏...cell提供一个方法
        if indexPath.row == 3 {
            cell.showBtns(false)
        } else {
            cell.showBtns(true)
        }
        
        cell.beginTryBlock = {
            print("开始体验")
            //获取当前app条转到将主页设置为跟控制器
//            let window: UIWindow =  ((UIApplication.sharedApplication().delegate?.window)!)!
//            window.rootViewController = KVBaseTabBarVC()
            AppDelegate.setupRootViewController(KVBaseTabBarVC())
        }
        return cell
    }
    // MARK: - 私有方法
    private func setupCellInfo() {
        layout.itemSize = self.view.bounds.size
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
//        UIScrollView
        self.collectionView?.pagingEnabled = true
        self.collectionView?.alwaysBounceHorizontal = true
        self.collectionView?.bounces = false
    }

}
